package poem_info2.Test;

import JdbcUtils.JDBCUtils;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import poem_info2.domain.Poem;

import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

public class Test {
    @org.junit.Test
    public void test() {
        JdbcTemplate template = new JdbcTemplate(JDBCUtils.getDataSource());
        List<Poem> list = template.query("select * from poems", new BeanPropertyRowMapper<Poem>(Poem.class));
//        for (Poem poem : list) {
//            System.out.println(poem.toString());
//        }
        Poem poem = new Poem();
        poem.setTitle("九月十日即事");
        poem.setDynasty_id(3);
        poem.setWriter_id(1);
        poem.setContent("昨日登高罢，今朝更举觞。 菊花何太苦，遭此两重阳？");
        for (Poem p : list) {
            if (p.equals(poem)) {
                System.out.println(poem);
            }
        }
    }

    @org.junit.Test
    public void test2() throws Exception {
        String url = "https://so.gushiwen.cn/guwen/bookv_46653FD803893E4F6BB4FDB169B583C3.aspx";
        Document document = Jsoup.parse(new URL(url), 10000);
        Elements ele1 = document.select("div.contson");
        if (ele1.isEmpty()) {
            System.out.println(true);
        }
        System.out.println(ele1.text());
    }

    @org.junit.Test
    public void test3() throws Exception {
        List<String> url_list = new ArrayList<String>();
        //解析每个朝代页面的url
        Document parse = Jsoup.parse(new URL("https://www.gushiwen.com/type/n/xianqin/n/n.html"), 10000);
        Elements hrefs = parse.select("div.link2 > a");
        for (Element href : hrefs) {
            String s = href.attr("href").split("/")[3];
            url_list.add(s);
            System.out.println(s);
        }

    }

    @org.junit.Test
    public void test4() throws Exception {
        List<String> type_list = new ArrayList<String>();
        //解析每个朝代页面的url
        Document parse = Jsoup.parse(new URL("https://www.gushiwen.com/type/n/xianqin/n/n.html"), 10000);
        Elements hrefs = parse.select("div.link2 > a");
        for (Element href : hrefs) {
            String s = href.attr("href").split("/")[3];
            type_list.add(s);
            System.out.println(s);
        }
        int i = 0;
        for (String ty : type_list) {
            //获取每个类别诗歌的url页面，里面包含了每首诗歌的详情页url
            System.out.println("-------------------------------------------------分割线-----------------------------------------------------");
            System.out.println(ty);
            if(ty.equals("xianqin") || ty.equals("lianghan")) continue;
            boolean flag = true;
            while (flag) {
                String url = "https://www.gushiwen.com/type/n/" + ty + "/n/" + ++i + ".html";
                System.out.println("正在解析" + url);
                Document parse1 = Jsoup.parse(new URL(url), 10000);
                String text = parse1.select("div.fenye > a").last().text();
                if(text.equals("最后页")) flag = true;
                else {
                    flag = false;
                }
            }
            System.out.println(ty + ":" + i);
            i = 0;
        }
    }

    @org.junit.Test
    public void test5() {
        JdbcTemplate template = new JdbcTemplate(JDBCUtils.getDataSource());
        String sql = "select id from writers where writer = ?";
        List<Map<String, Object>> list = template.queryForList(sql, "李白");
//        for (Map<String, Object> l:list) {
//            for (String s : l.keySet()) {
//                System.out.println(s);
//            }
//        }
    }
}
